package com.childmiles.gwt.server;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.childmiles.gwt.client.domain.AwardWinner;
import com.childmiles.gwt.client.service.AwardWinnerService;
import com.childmiles.service.persistence.Dao;
import com.childmiles.service.persistence.QuickAndDirtyDaoImpl;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AwardWinnerServiceImpl extends RemoteServiceServlet implements AwardWinnerService {
	private static final long serialVersionUID = 1L;
	private Dao dao = new QuickAndDirtyDaoImpl();
	
	public void save(AwardWinner awardWinner) {
	    dao.save(awardWinner);
	}
	
	public AwardWinner find(String name) {
	    return dao.find(name);
	}

	/**
	 * Sorted by number of awards
	 */
    public List<AwardWinner> findAll() {
        List<AwardWinner> awardWinners = dao.findAll();
        Collections.sort(awardWinners, new Comparator<AwardWinner>() {
            public int compare(AwardWinner o1, AwardWinner o2) {
                return o2.getAwards().getTotalNumberOfAwards().compareTo(o1.getAwards().getTotalNumberOfAwards());
            }
        });
        return awardWinners;
    }

}
